package com.dcdata.jt809server.client;

import ch.qos.logback.core.encoder.ByteArrayUtil;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.net.InetSocketAddress;

/**
 * @author choice
 * @date 2019-03-13 14:40
 *
 */
public class JT809TcpClient {
    public void runClient() throws Exception{
        //如果现在客户端不同，那么也可以不实用多线程模式来处理
        //在netty中考虑到代码的统一性，也允许你在客户端设置线程池
        EventLoopGroup group = new NioEventLoopGroup();//创建一个线程池
        try {
            Bootstrap client = new Bootstrap();//创建客户毒案处理程序
            client.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY,true) //允许接收大块的返回数据
                    .handler(new ChannelInitializer<SocketChannel>() {

                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new FixedLengthFrameDecoder(1000));
                            socketChannel.pipeline().addLast(new Message2ByteEncoder());
                            socketChannel.pipeline().addLast(new Byte2MessageDecoder());
                            socketChannel.pipeline().addLast(new JT809TcpClientHandler());
                        }
                    });
            //ChannelFuture channelFuture = client.connect(new InetSocketAddress("103.242.175.154", 8001)).sync();//等待连接处理
            ChannelFuture channelFuture = client.connect(new InetSocketAddress("127.0.0.1", 8001)).sync();//等待连接处理
            channelFuture.addListener(new GenericFutureListener() {
                @Override
                public void operationComplete(Future future) throws InterruptedException {
                    if(future.isSuccess()){
                        //while (true) {
                            Thread.sleep(500);
//                            channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray("5B0000005A020000006912000000000B1000000000000000B8D3423633323635000000000000000000000000000212020000002400070C07E20E223806D7DC4D0185820F005200000079A079015006C2000C000300000000BCFD5D"));
//                        channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray("5B0000005A0000011812000001E2400100010000000000C1C9434437313635000000000000000000000000000212020000002400090707E31234230736F0DE026ECFEF00000000000184E9010C000E000C00030000000012725D"));
//                        channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray("5B0000001A0000257A10050001E24001000100000000001C525D"));
//                            channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray("5b000000480000001a100102a2494401020f010000ea1fe6442ccb6d10e15e023139d9088a6dbee820d8a686e895fbe79266a0928d3ce16fe02754e3774ca58195acc6ccb8ea04fb5d"));
                        channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray("5b0000004800000b3b100102a2494401020f0100006d8e85700ee34cb581977ea216ede361559d74432cc181ceeca24c1cc22db2a1929faf67a5420588943bd01964a8f95e01cdb65d"));
                            System.out.println("服务器连接已经完成，可以确保进行消息准确传输");
                        //}
                    }
                }
            });
            channelFuture.channel().closeFuture().sync();  //等待关闭连接

        }finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new JT809TcpClient().runClient();


    }
}
